#!/bin/bash
#
# https://github.com/Aniverse/inexistence
# Author: Aniverse
#
# script_update=2021.07.30
# script_version=r12047
# This var will recover other script's var when source
################################################################################################ Usage Guide

usage_guide() {
    s=/etc/inexistence/00.Installation/ask;rm -f $s ; nano $s
    wget -q https://github.com/Aniverse/inexistence/raw/master/00.Installation/ask -O /etc/inexistence/00.Installation/ask

    if [[ -f /etc/inexistence/00.Installation/ask ]]; then
        source /etc/inexistence/00.Installation/ask
    else
        source <(wget -qO- https://github.com/Aniverse/inexistence/raw/master/00.Installation/ask)
    fi
}

################################################################################################

#[[ $script_lang == eng ]] &&
#[[ $script_lang == chs ]] &&

function set_language() {
    if [[ $script_lang == eng ]]; then
        lang_do_not_install="Do not install"
        language_select_another_version="Select another version"
        which_version_do_you_want="Which version do you want?"
        lang_yizhuang="You have already installed"
        lang_will_be_installed="will be installed"
        lang_note_that="Note that"
        lang_would_you_like_to_install="Would you like to install"
    elif [[ $script_lang == chs ]]; then
        lang_do_not_install="我不想安装"
        language_select_another_version="以上版本都不要，我要另选一个版本"
        which_version_do_you_want="你想要装什么版本？"
        lang_yizhuang="你已经安装了"
        lang_will_be_installed="将会被安装"
        lang_note_that="注意"
        lang_would_you_like_to_install="是否需要安装"
    fi
}

################################################################################################

### 输入自己想要的软件版本 ###
# ${blue}(use it at your own risk)${normal}
function _input_version() {
    if [[ $script_lang == eng ]]; then
        echo -e "\n${JG} ${bold}Use it at your own risk and make sure to input version correctly${normal}"
        read -ep "${bold}${yellow}Input the version you want: ${cyan}" input_version_num; echo -n "${normal}"
    elif [[ $script_lang == chs ]]; then
        echo -e "\n${JG} ${bold}确保你输入的版本号能用，不然输错了脚本也不管的${normal}"
        read -ep "${bold}${yellow}输入你想要的版本号： ${cyan}" input_version_num; echo -n "${normal}"
    fi
}

function ask_multi_threads() {
    while [[ -z $MAXCPUS ]]; do
        echo -e "${green}01)${normal} Use ${cyan}all${normal} available threads (Default)"
        echo -e "${green}02)${normal} Use ${cyan}half${normal} of available threads"
        echo -e "${green}03)${normal} Use ${cyan}one${normal} thread"
        echo -e "${green}04)${normal} Use ${cyan}two${normal} threads"
        read -ep "${bold}${yellow}How many threads do you want to use when compiling?${normal} (Default ${cyan}01${normal}): " version
        case $responce in
            01 | 1 | "") MAXCPUS=$(nproc) ;;
            02 | 2     ) MAXCPUS=$(echo "$(nproc) / 2"|bc) ;;
            03 | 3     ) MAXCPUS=1 ;;
            04 | 4     ) MAXCPUS=2 ;;
            *          ) MAXCPUS=$(nproc) ;;
        esac
    done
    echo -e "${bold}${baiqingse}[${MAXCPUS}]${normal} ${bold}thread(s) will be used when compiling${normal}\n"
}

function ask_swap() {
    [[ -d /proc/vz ]] && [[ $tram -le 1926 ]] && {
    echo -e "${JG} You're using OpenVZ VPS and your RAM is less than 2GB\nYour memory may got exhausted sometimes when running this script\n"
    USESWAP=OpenVZ ; }
    if [[ -z $USESWAP ]] && [[ $tram -le 1926 ]]; then
        echo -e  "${bold}${red}$lang_note_that${normal} ${bold}Your RAM is below ${red}1926MB${jiacu}, memory may got exhausted when compiling${normal}"
        read -ep "${bold}${yellow}Would you like to use swap when compiling?${normal} [${cyan}Y${normal}]es or [N]o: " version
        case $responce in
            [yY] | [yY][Ee][Ss] | "") USESWAP=Yes ;;
            [nN] | [nN][Oo]         ) USESWAP=No  ;;
            *                       ) USESWAP=Yes ;;
        esac
        if [[ $USESWAP == Yes ]]; then
            echo -e "${bold}${baiqingse} 1GB Swap ${normal} will be used\n"
        else
            echo -e "${bold}Swap will not be used${normal}\n"
        fi
    fi
}

function ask_apt_sources() {
    while [[ -z $aptsources ]]; do
        read -ep "${bold}${yellow}Would you like to change sources list?${normal}  [${cyan}Y${normal}]es or [N]o: " responce
      # echo -ne "${bold}${yellow}Would you like to change sources list?${normal} [${cyan}Y${normal}]es or [N]o: " ; read -e responce
        case $responce in
            [yY] | [yY][Ee][Ss] | ""   ) aptsources=Yes ;;
            [nN] | [nN][Oo]            ) aptsources=No ;;
            *                          ) aptsources=Yes ;;
        esac
    done
    if [[ $aptsources == Yes ]]; then
        echo "${bold}${baiqingse}/etc/apt/sources.list${normal} ${bold}will be replaced${normal}"
    else
        echo "${baizise}/etc/apt/sources.list will ${baihongse}not${baizise} be replaced${normal}"
    fi
    echo
}

# qb_compile_available
# wget -qO- -o /dev/null "https://github.com/qbittorrent/qBittorrent/releases" | grep -Eo "release-[0-9.]+" | sed -e "s/\.$//" -e "s/release-//" | sort -u | pr -6 -t ; echo
function ask_qbittorrent() {
    while [[ -z $qb_version ]]; do
        echo -e "${green}01)${normal} qBittorrent ${cyan}4.1.9 ${normal}(static)${normal}"
        echo -e "${green}11)${normal} qBittorrent ${cyan}4.1.9 ${normal}(deb)${normal}"
        echo -e "${green}12)${normal} qBittorrent ${cyan}4.2.5 ${normal}(deb)${normal}"
        echo -e "${green}21)${normal} qBittorrent ${cyan}4.3.6 ${normal} with libtorrent 1.2.14 (static)${normal}"
        echo -e "${green}22)${normal} qBittorrent ${cyan}4.3.6 ${normal} with libtorrent 2.0.4  (static)${normal}"
        echo -e  "${blue}30)${normal} $language_select_another_version"
        echo -e   "${red}99)${normal} $lang_do_not_install qBittorrent"

        [[ -n $qb_installed_ver ]] &&
        echo -e "${bailanse}${bold} ATTENTION ${normal} ${blue}${bold}$lang_yizhuang ${underline}qBittorrent ${qb_installed_ver}${normal}"
        read -ep "${bold}${yellow}$which_version_do_you_want${normal} (Default ${cyan}12${normal}): " version
        case $version in
            01 | 1) qb_version=4.1.9 ; qb_mode=static ;;
            11) qb_version=4.1.9    ;;
            12) qb_version=4.2.5    ;;
            21) qb_version=4.3.6.lt1 ; qb_mode=static ;;
            22) qb_version=4.3.6.lt2 ; qb_mode=static ;;
            30) _input_version && qb_version="${input_version_num}" ;;
            99) qb_version=no ;;
            * | "") qb_version=4.2.5 ;;
        esac
    done

    if [[ $qb_version == no ]]; then
        echo -e "${baizise}qBittorrent will ${baihongse}not${baizise} be installed${normal}\n"
    else
        echo -e "${bold}${baiqingse}qBittorrent ${qb_version}${normal} ${bold}$lang_will_be_installed${normal}\n"
    fi
}

# wget -qO- "http://download.deluge-torrent.org/source/" | grep -Eo "1\.3\.[0-9]+" | sort -u | pr -6 -t ; echo
function ask_deluge() {
    while [[ -z $de_version ]]; do
        echo -e "${green}01)${normal} Deluge ${cyan}1.3.15${normal}"
        echo -e  "${blue}02)${normal} Deluge ${blue}2.0.3${normal} (Python 2.7)"
        echo -e  "${blue}30)${normal} $language_select_another_version"
        echo -e   "${red}99)${normal} $lang_do_not_install Deluge"

        [[ -n $de_installed_ver ]] &&
        echo -e "${bailanse}${bold} ATTENTION ${normal} ${blue}${bold}$lang_yizhuang ${underline}Deluge ${de_installed_ver}${reset_underline}${normal}"

        read -ep "${bold}${yellow}$which_version_do_you_want${normal} (Default ${cyan}01${normal}): " version
        case $version in
            01 | 1) de_version=1.3.15 ;;
            02 | 2) de_version=2.0.3 ;;
            21) de_version='1.3.15_skip_hash_check' ;;
            30) _input_version && de_version="${input_version_num}" ;;
            31) _input_version && de_version="${input_version_num}" && de_test=yes &&  de_branch=yes ;;
            32) _input_version && de_version="${input_version_num}" && de_test=yes && de_version=yes ;;
            99) de_version=no ;;
            * | "") de_version=1.3.15 ;;
        esac
    done

    Deluge_2_later=No
    [[ $(echo $de_version | grep -oP "[0-9.]+") ]] && { version_ge $de_version 1.3.11 || Deluge_ssl_fix_patch=Yes ; }
    [[ $(echo $de_version | grep -oP "[0-9.]+") ]] && version_ge $de_version 2.0 && Deluge_2_later=Yes
    [[ $de_version == '1.3.15_skip_hash_check'  ]] && Deluge_1_3_15_skip_hash_check_patch=Yes

    if [[ $de_version == no ]]; then
        echo -e "${baizise}Deluge will ${baihongse}not${baizise} be installed${normal}\n"
    else
        echo -e "${bold}${baiqingse}Deluge ${de_version}${normal} ${bold}$lang_will_be_installed${normal}\n"
    fi
}

function ask_rtorrent() {
    if [[ $script_lang == eng ]]; then
        local lang_ipv6_1="with IPv6 support"
        local lang_ipv6_2="with UNOFFICAL IPv6 support"
        local lang_3="released on Sep 04, 2015"
        local lang_4="feature-bind branch on Jan 30, 2018"
        local branch=branch
    elif [[ $script_lang == chs ]]; then
        local lang_ipv6_1="支持 IPv6"
        local lang_ipv6_2="支持 IPv6 的修改版"
        local lang_3="2015 年的正式发布版本"
        local lang_4="2018 年 1 月 的 feature-bind 分支版本"
        local branch="分支"
    fi

    while [[ -z $rt_version ]]; do
            [[ $rtorrent_dev != 1 ]] && {
            echo -e "${green}01)${normal} rTorrent ${cyan}0.9.2${normal}"
            echo -e "${green}02)${normal} rTorrent ${cyan}0.9.3${normal}"
            echo -e "${green}03)${normal} rTorrent ${cyan}0.9.4${normal}"
            echo -e "${green}04)${normal} rTorrent ${cyan}0.9.6${normal} ($lang_3)"
            echo -e "${green}11)${normal} rTorrent ${cyan}0.9.2${normal} ($lang_ipv6_1)"
            echo -e "${green}12)${normal} rTorrent ${cyan}0.9.3${normal} ($lang_ipv6_1)"
            echo -e "${green}13)${normal} rTorrent ${cyan}0.9.4${normal} ($lang_ipv6_1)" ; }
            echo -e "${green}14)${normal} rTorrent ${cyan}0.9.6${normal} ($lang_4)"
            echo -e "${green}15)${normal} rTorrent ${cyan}0.9.7${normal} ($lang_ipv6_1)"
            echo -e "${green}16)${normal} rTorrent ${cyan}0.9.8${normal} ($lang_ipv6_1)"
            echo -e "${green}17)${normal} rTorrent ${cyan}feature-bind branch${normal} ($lang_ipv6_1)"
            echo -e   "${red}99)${normal} $lang_do_not_install rTorrent"
    
            [[ -n $rt_installed_ver ]] &&
            echo -e "${bailanse}${bold} ATTENTION ${normal} ${blue}${bold}$lang_yizhuang ${underline}rTorrent ${rt_installed_ver}${normal}"
            [[ $rtorrent_dev == 1 ]] &&
            echo "${bold}${red}$lang_note_that${normal} ${bold}${green}Debian 9/10${jiacu} and ${green}Ubuntu 18.04 ${jiacu}is only supported by ${green}rTorrent 0.9.6 and later${normal}"
    
            read -ep "${bold}${yellow}$which_version_do_you_want${normal} (Default ${cyan}14${normal}): " version
            case $version in
                01 | 1) rt_version=0.9.2 ;;
                02 | 2) rt_version=0.9.3 ;;
                03 | 3) rt_version=0.9.4 ;;
                04 | 4) rt_version=0.9.6 ;;
                11) rt_version='0.9.2 IPv6 supported' ;;
                12) rt_version='0.9.3 IPv6 supported' ;;
                13) rt_version='0.9.4 IPv6 supported' ;;
                14) rt_version='0.9.6 IPv6 supported' ;;
                15) rt_version=0.9.7 ;;
                16) rt_version=0.9.8 ;;
                17) rt_version=feature ;;
                99) rt_version=no ;;
                "" | *) rt_version='0.9.6 IPv6 supported' ;;
            esac
    done

    [[ $IPv6Opt == -i ]] && rt_version=`echo $rt_version IPv6 supported`
    [[ `echo $rt_version | grep IPv6` ]] && IPv6Opt=-i
    [[ $rt_version =~ (0.9.7|0.9.8|feature) ]] && IPv6Opt=-i
    rt_versionIns=`echo $rt_version | grep -Eo [0-9].[0-9].[0-9]`
    [[ $rt_version == feature ]] && rt_versionIns=feature

    if [[ $rt_version == no ]]; then
        [[ $script_lang == eng ]] && echo "${baizise}rTorrent will ${baihongse}not${baizise} be installed${normal}"
        [[ $script_lang == chs ]] && echo "${baihongse}跳过${baizise} rTorrent 的安装${normal}"
        InsFlood='No rTorrent'
    else
        if [[ `echo $rt_version | grep IPv6 | grep -Eo 0.9.[234]` ]]; then
            echo "${bold}${baiqingse}rTorrent $rt_versionIns ($lang_ipv6_2)${normal} ${bold}$lang_will_be_installed${normal}"
        elif [[ $rt_version == '0.9.6 IPv6 supported' ]]; then
            echo "${bold}${baiqingse}rTorrent 0.9.6 (feature-bind $branch)${normal} ${bold}$lang_will_be_installed${normal}"
        else
            echo "${bold}${baiqingse}rTorrent ${rt_version}${normal} ${bold}$lang_will_be_installed${normal}"
        fi
    fi

    echo
}

function ask_flood() {
    while [[ -z $InsFlood ]]; do
        read -ep "${bold}${yellow}$lang_would_you_like_to_install flood? ${normal} [Y]es or [${cyan}N${normal}]o: " responce
        case $responce in
            [yY] | [yY][Ee][Ss]  ) InsFlood=Yes ;;
            [nN] | [nN][Oo] | "" ) InsFlood=No  ;;
            *) InsFlood=No ;;
        esac
    done
    if [[ $InsFlood == Yes ]]; then
        echo -e "${bold}${baiqingse}Flood${normal} ${bold}$lang_will_be_installed${normal}\n"
    else
        echo -e "${baizise}Flood will ${baihongse}not${baizise} be installed${normal}\n"
    fi
}

function ask_transmission() {
    while [[ -z $tr_version ]]; do

        echo -e "${green}01)${normal} Transmission ${cyan}2.94${normal} (compile)"
        echo -e "${green}11)${normal} Transmission ${cyan}2.94${normal} (deb)"
        echo -e  "${blue}30)${normal} $language_select_another_version"
        echo -e   "${red}99)${normal} $lang_do_not_install Transmission"

        [[ -n $tr_installed_ver ]] &&
        echo -e "${bailanse}${bold} ATTENTION ${normal} ${blue}${bold}$lang_yizhuang ${underline}Transmission ${tr_installed_ver}${normal}"

        read -ep "${bold}${yellow}$which_version_do_you_want${normal} (Default ${cyan}11${normal}): " version

        case $version in
                01 | 1) tr_version=2.94 ;;
                11) tr_version=2.94 && TRdefault=deb ;;
                30) _input_version && tr_version="${input_version_num}" ;;
                99) tr_version=no ;;
                "" | *) tr_version=2.94 && TRdefault=deb ;;
        esac
    
    done

    if [[ $tr_version == no ]]; then
        echo -e "${baizise}Transmission will ${baihongse}not${baizise} be installed${normal}\n"
    else
        echo -e "${bold}${baiqingse}Transmission ${tr_version}${normal} ${bold}$lang_will_be_installed${normal}\n"
    fi
}

################################################################################################
################################################################################################
################################################################################################

function ask_flexget() {
    while [[ -z $InsFlex ]]; do
        [[ $fg_installed == Yes ]] && echo -e "${bailanse}${bold} ATTENTION ${normal} ${blue}${bold}$lang_yizhuang FlexGet${normal}"
        read -ep "${bold}${yellow}$lang_would_you_like_to_install FlexGet?${normal} [Y]es or [${cyan}N${normal}]o: " responce
        case $responce in
            [yY] | [yY][Ee][Ss]  ) InsFlex=Yes ;;
            [nN] | [nN][Oo] | "" ) InsFlex=No ;;
            *) InsFlex=No ;;
        esac
    done
    if [ $InsFlex == Yes ]; then
        echo -e "${bold}${baiqingse}Flexget${normal} ${bold}$lang_will_be_installed${normal}\n"
    else
        echo -e "${baizise}Flexget will ${baihongse}not${baizise} be installed${normal}\n"
    fi
}

function ask_rclone() {
    while [[ -z $InsRclone ]]; do
        [[ $rclone_installed == Yes ]] && echo -e "${bailanse}${bold} ATTENTION ${normal} ${blue}${bold}$lang_yizhuang rclone${normal}"
        read -ep "${bold}${yellow}$lang_would_you_like_to_install rclone?${normal} [Y]es or [${cyan}N${normal}]o: " responce
        case $responce in
            [yY] | [yY][Ee][Ss]  ) InsRclone=Yes ;;
            [nN] | [nN][Oo] | "" ) InsRclone=No  ;;
            *) InsRclone=No ;;
        esac
    done
    if [[ $InsRclone == Yes ]]; then
        echo -e "${bold}${baiqingse}rclone${normal} ${bold}$lang_will_be_installed${normal}\n"
    else
        echo -e "${baizise}rclone will ${baihongse}not${baizise} be installed${normal}\n"
    fi
}

function ask_rdp() {
    while [[ -z $InsRDP ]]; do
        echo -e "${green}01)${normal} noVNC with xfce4"
        echo -e "${green}02)${normal} X2Go with xfce4"
        echo -e   "${red}99)${normal} $lang_do_not_install remote desktop"
        read -ep "${bold}${yellow}$lang_would_you_like_to_install remote desktop?${normal} (Default ${cyan}99${normal}): " responce
        case $responce in
            01 | 1) InsRDP=VNC  ;;
            02 | 2) InsRDP=X2Go ;;
            99    ) InsRDP=No   ;;
            "" | *) InsRDP=No   ;;
        esac
    done
    if [[ $InsRDP == VNC ]]; then
        echo -e "${bold}${baiqingse}VNC${jiacu} and ${baiqingse}xfce4${jiacu} $lang_will_be_installed${normal}\n"
    elif [[ $InsRDP == X2Go ]]; then
        echo -e "${bold}${baiqingse}X2Go${normal} and ${bold}${baiqingse}xfce4${jiacu} $lang_will_be_installed${normal}\n"
    else
        echo -e "${baizise}VNC or X2Go will ${baihongse}not${baizise} be installed${normal}\n"
    fi
}

function ask_wine() {
    while [[ -z $InsWine ]]; do
        read -ep "${bold}${yellow}$lang_would_you_like_to_install wine?${normal} [Y]es or [${cyan}N${normal}]o: " responce
        case $responce in
            [yY] | [yY][Ee][Ss]  ) InsWine=Yes ;;
            [nN] | [nN][Oo] | "" ) InsWine=No  ;;
            *) InsWine=No ;;
        esac
    done
    if [[ $InsWine == Yes ]]; then
        echo -e "${bold}${baiqingse}Wine${jiacu} $lang_will_be_installed${normal}\n"
    else
        echo -e "${baizise}Wine will ${baihongse}not${baizise} be installed${normal}\n"
    fi
}

function ask_mono() {
    while [[ -z $InsMono ]]; do
        read -ep "${bold}${yellow}$lang_would_you_like_to_install mono?${normal} [Y]es or [${cyan}N${normal}]o: " responce
        case $responce in
            [yY] | [yY][Ee][Ss]  ) InsMono=Yes ;;
            [nN] | [nN][Oo] | "" ) InsMono=No  ;;
            *) InsMono=No ;;
        esac
    done
    if [[ $InsMono == Yes ]]; then
        echo -e "${bold}${baiqingse}Mono${jiacu} $lang_will_be_installed${normal}\n"
    else
        echo -e "${baizise}Mono will ${baihongse}not${baizise} be installed${normal}\n"
    fi
}

function ask_tools() {
    while [[ -z $InsTools  ]]; do
        echo -e "MKVToolnix, mktorrent, eac3to, mediainfo, ffmpeg ..."
        echo -ne "${bold}${yellow}$lang_would_you_like_to_install the above additional softwares?${normal} [Y]es or [${cyan}N${normal}]o: " ; read -e responce
        case $responce in
            [yY] | [yY][Ee][Ss]  ) InsTools=Yes ;;
            [nN] | [nN][Oo] | "" ) InsTools=No  ;;
           *) InsTools=No ;;
        esac
    done
    if [[ $InsTools == Yes ]]; then
        echo -e "${bold}${baiqingse}Latest version of these softwares${jiacu} $lang_will_be_installed${normal}\n"
    else
        echo -e "${baizise}These softwares will ${baihongse}not${baizise} be installed${normal}\n"
    fi
}

function ask_tweaks() {
    while [[ $UseTweaks = "" ]]; do
        echo -ne "${bold}${yellow}Would you like to do some system tweaks? ${normal} [${cyan}Y${normal}]es or [N]o: " ; read -e responce
        case $responce in
            [yY] | [yY][Ee][Ss] | "" ) UseTweaks=Yes ;;
            [nN] | [nN][Oo]          ) UseTweaks=No ;;
            *                        ) UseTweaks=Yes ;;
        esac
    done
    if [[ $UseTweaks == Yes ]]; then
        echo -e "${bold}${baiqingse}System tweaks${normal} ${bold}will be configured${normal}\n"
    else
        echo -e "${baizise}System tweaks will ${baihongse}not${baizise} be configured${normal}\n"
    fi
}

function ask_reboot() {
    if [[ $script_lang == eng ]]; then
        local lang_1="Would you like to reboot the system now?"
        local lang_2="WTF, try reboot manually?"
        local lang_3="Reboot has been canceled..."
    elif [[ $script_lang == chs ]]; then
        local lang_1="你现在想要重启系统么？"
        local lang_2="emmmm，重启失败，你手动重启试试？"
        local lang_3="已取消重启……"
    fi
    if [[ $NoReboot != 1 ]]; then
        echo -ne "${bold}${yellow}$lang_1 ${normal} [y/${cyan}N${normal}]: "
        if [[ $ForceYes == 1 ]];then
            reboot || echo "$lang_2"
        else
            read -e is_reboot
        fi
        if [[ $is_reboot == "y" || $is_reboot == "Y" ]]; then
            reboot
        else
            echo -e "${bold}$lang_3${normal}\n"
        fi
    fi
}



################################################################################################
################################################################################################ NEW FUNCTIONS after creating this script
################################################################################################

function ask_filebrowser() {
    while [[ -z $InsFB ]]; do
        read -ep "${bold}${yellow}$lang_would_you_like_to_install FileBrowser? ${normal} [${cyan}Y${normal}]es or [N]o: " responce
        case $responce in
            [yY] | [yY][Ee][Ss] | "" ) InsFB=Yes ;;
            [nN] | [nN][Oo]          ) InsFB=No  ;;
                                    *) InsFB=Yes ;;
        esac
    done
    if [[ $InsFB == Yes ]]; then
        echo -e "${bold}${baiqingse}FileBrowser${normal} ${bold}$lang_will_be_installed${normal}\n"
    else
        echo -e "${baizise}FileBrowser will ${baihongse}not${baizise} be installed${normal}\n"
    fi
}

################################################################################################
################################################################################################ WILL BE DEPRECATED SOON
################################################################################################

# 检查是否已经启用BBR、BBR 魔改版
function check_bbr_status() {
    tcp_control=$(cat /proc/sys/net/ipv4/tcp_congestion_control)
    [[ $tcp_control =~ (bbr|bbr_powered|bbrplus|nanqinlang|tsunami) ]] && bbrinuse=Yes || bbrinuse=No
}

# 检查理论上内核是否支持原版 BBR
function check_kernel_version() {
    kernel_vvv=$(uname -r | cut -d- -f1)
    [[ -n $kernel_vvv ]] && version_ge $kernel_vvv 4.9 && bbrkernel=Yes || bbrkernel=No
    # ls /lib/modules/\$(uname -r)/kernel/net/ipv4 | grep tcp_bbr.ko -q
}

# 询问是否安装BBR
function ask_bbr() {
    check_bbr_status
    if [[ $bbrinuse == Yes ]]; then
        echo -e "${bold}${yellow}TCP BBR has been installed. Skip ...${normal}"
        InsBBR=Already\ Installed
    elif [[ -d /proc/vz ]]; then
        echo -e "${yellow}${bold}Since your seedbox is based on ${red}OpenVZ${normal}${yellow}${bold}, skip BBR installation${normal}\n"
        InsBBR=Not\ supported\ on\ OpenVZ
    else
        check_kernel_version
        while [[ -z $InsBBR ]]; do
            if [[ $bbrkernel == Yes ]]; then
                echo -e "${bold}Your kernel is newer than ${green}4.9${normal}${bold}, but BBR is not enabled${normal}"
                read -ep "${bold}${yellow}Would you like to use BBR? ${normal} [${cyan}Y${normal}]es or [N]o: " responce
                case $responce in
                    [yY] | [yY][Ee][Ss] | "" ) InsBBR=To\ be\ enabled ;;
                    [nN] | [nN][Oo]          ) InsBBR=No ;;
                    *                        ) InsBBR=To\ be\ enabled ;;
                esac
            else
                echo -e "A new kernel (4.11.12) $lang_will_be_installed if BBR is to be installed"
                echo -e "${red}WARNING${normal} ${bold}Installing new kernel may cause reboot failure in some cases${normal}"
                echo -ne "${bold}${yellow}$lang_would_you_like_to_install BBR? ${normal} [Y]es or [${cyan}N${normal}]o: " ; read -e responce
                case $responce in
                    [yY] | [yY][Ee][Ss]  ) InsBBR=Yes ;;
                    [nN] | [nN][Oo] | "" ) InsBBR=No ;;
                    *                    ) InsBBR=No ;;
                esac
            fi
        done

       [[ $InsBBR == Yes ]] && [[ $bbrkernel == Yes ]] && InsBBR=To\ be\ enabled
        if [[ $InsBBR == Yes ]]; then
            echo -e "${bold}${baiqingse}TCP BBR${normal} ${bold}$lang_will_be_installed${normal}\n"
        elif [[ $InsBBR == To\ be\ enabled ]]; then
            echo -e "${bold}${baiqingse}TCP BBR${normal} ${bold}will be enabled${normal}\n"
        else
            echo -e "${baizise}TCP BBR will ${baihongse}not${baizise} be installed${normal}\n"
        fi
    fi
}

################################################################################################
################################################################################################ temporary
################################################################################################

function install_deluge() {

    if [[ $separate == 1 ]] ; then
        bash $local_packages/package/deluge/install -v $de_version
    else
        apt-get install -y python python-pip python-setuptools
        apt-get install -y python-twisted python-openssl python-xdg python-chardet geoip-database python-notify python-pygame python-glade2 librsvg2-common xdg-utils python-mako
        if [[ $Deluge_2_later == Yes ]]; then
            apt-get install -y python-pip
            pip2 install --upgrade pip
            hash -d pip2
            pip2 install --upgrade setuptools
            pip2 install --upgrade twisted[tls] twisted pillow rencode pyopenssl
            pip2 install --upgrade setproctitle
        fi
        cd $SourceLocation

        if [[ $de_version == 2.0.3 ]]; then
            while true ; do
                wget -nv -N https://ftp.osuosl.org/pub/deluge/source/2.0/deluge-2.0.3.tar.xz && break
                sleep 1
            done
            tar xf deluge-$de_version.tar.xz
            rm -f deluge-$de_version.tar.xz
            cd deluge-2.0.3
        else
            [[ $Deluge_1_3_15_skip_hash_check_patch == Yes  ]] && de_version=1.3.15
            while true ; do
                wget -nv -N https://github.com/deluge-torrent/deluge/archive/deluge-$de_version.tar.gz && break
                sleep 1
            done
            tar xf deluge-$de_version.tar.gz
            rm -f deluge-$de_version.tar.gz
            cd deluge*$de_version
        fi

        ### 修复稍微新一点的系统（比如 Debian 8）（Ubuntu 14.04 没问题）下 RPC 连接不上的问题。这个问题在 Deluge 1.3.11 上已解决
        ### http://dev.deluge-torrent.org/attachment/ticket/2555/no-sslv3.diff
        ### https://github.com/deluge-torrent/deluge/blob/deluge-1.3.9/deluge/core/rpcserver.py
        ### https://github.com/deluge-torrent/deluge/blob/deluge-1.3.11/deluge/core/rpcserver.py
        if [[ $Deluge_ssl_fix_patch == Yes ]]; then
            sed -i "s/SSL.SSLv3_METHOD/SSL.SSLv23_METHOD/g" deluge/core/rpcserver.py
            sed -i "/        ctx = SSL.Context(SSL.SSLv23_METHOD)/a\        ctx.set_options(SSL.OP_NO_SSLv2 & SSL.OP_NO_SSLv3)" deluge/core/rpcserver.py
            echo -e "\n\nSSL FIX (FOR LOG)\n\n"
            python setup.py build  > /dev/null
            python setup.py install --install-layout=deb --record /log/inexistence/deluge_filelist.txt > /dev/null
            mv -f /usr/bin/deluged /usr/bin/deluged2
            wget -nv -N http://download.deluge-torrent.org/source/deluge-1.3.15.tar.gz
            tar xf deluge-1.3.15.tar.gz && rm -f deluge-1.3.15.tar.gz && cd deluge-1.3.15
        fi

        python setup.py build  > /dev/null
        python setup.py install --install-layout=deb --record $LogLocation/install_deluge_filelist_$de_version.txt  > /dev/null # 输出太长了，省略大部分，反正也不重要
        python setup.py install_data # For Desktop users

        if [[ $Deluge_1_3_15_skip_hash_check_patch == Yes ]]; then
            wget https://raw.githubusercontent.com/Aniverse/inexistence/files/miscellaneous/deluge.1.3.15.skip.no.full.allocate.patch \
            -O /tmp/deluge.1.3.15.skip.no.full.allocate.patch
            cd /usr/lib/python2.7/dist-packages/deluge-1.3.15-py2.7.egg/deluge
            patch -p1 < /tmp/deluge.1.3.15.skip.no.full.allocate.patch
        fi
        # 这个私货是修改 Deluge WebUI 里各个标签的默认排序以及宽度，符合我个人的习惯（默认的简直没法用，每次都要改很麻烦）
        if [[ $sihuo == yes ]]; then
            wget -nv https://github.com/Aniverse/inexistence/raw/files/miscellaneous/deluge.status.bar.patch \
            -O /tmp/deluge.status.bar.patch
            cd /usr/lib/python2.7/dist-packages/deluge-${de_version}-py2.7.egg/deluge
            patch -p1 < /tmp/deluge.status.bar.patch
        fi
        [[ $Deluge_ssl_fix_patch == Yes ]] && mv -f /usr/bin/deluged2 /usr/bin/deluged # 让老版本 Deluged 保留，其他用新版本
    fi
}

function install_flood() {
    # https://github.com/nodesource/distributions/blob/master/README.md
    curl -sL https://deb.nodesource.com/setup_10.x | bash -
    apt-get install -y nodejs build-essential # python3-dev
    npm install -g node-gyp
    git clone --depth=1 https://github.com/jfurrow/flood.git /srv/flood
    cd /srv/flood
    cp config.template.js config.js
    npm install
    sed -i "s/127.0.0.1/0.0.0.0/" /srv/flood/config.js

    npm run build 2>&1 | tee /tmp/flood.log
    rm -rf $LOCKLocation/flood.fail.lock
    # [[ `grep "npm ERR!" /tmp/flood.log` ]] && touch $LOCKLocation/flood.fail.lock

    cat << EOF > /etc/systemd/system/flood.service
[Unit]
Description=Flood rTorrent Web UI
After=network.target

[Service]
User=root
WorkingDirectory=/srv/flood
ExecStart=/usr/bin/npm start

[Install]
WantedBy=multi-user.target
EOF
    systemctl start  flood
    systemctl enable flood

    touch $LOCKLocation/flood.lock
    cd
}

function install_rtorrent() {
    bash -c "$(wget -qO- https://raw.githubusercontent.com/Aniverse/rtinst/master/rtsetup)"
    sed -i "s/make\ \-s\ \-j\$(nproc)/make\ \-s\ \-j${MAXCPUS}/g" /usr/local/bin/rtupdate

    if [[ $rt_installed == Yes ]]; then
        rtupdate $IPv6Opt $rt_versionIns
    else
        rtinst --ssh-default --ftp-default --rutorrent-master --force-yes --log $IPv6Opt -v $rt_versionIns -u $iUser -p $iPass -w $iPass
    fi

    if [[ -n "$rt_domain" ]] && [[ $(systemctl is-active nginx 2>/dev/null) == active ]]; then
        cd ; wget -O- https://raw.githubusercontent.com/acmesh-official/acme.sh/master/acme.sh | INSTALLONLINE=1  sh
        /root/.acme.sh/acme.sh --issue -d $rt_domain --webroot /var/www && \
        /root/.acme.sh/acme.sh --installcert -d $rt_domain \
        --key-file   /etc/ssl/private/ruweb.key \
        --fullchain-file /etc/ssl/ruweb.crt \
        --reloadcmd  "service nginx force-reload"
        rm -f master.tar.gz
    fi

    mv /root/rtinst.log $LogLocation/07.rtinst.script.log
    mv /home/$iUser/rtinst.info $LogLocation/07.rtinst.info.txt
    ln -s /home/${iUser} $WebROOT/h5ai/$iUser/user.root
    touch $LOCKLocation/rtorrent.lock
}

function install_transmission() {
    if [[ "${tr_version}" == 2.94 ]] && [[ "${TRdefault}" == deb ]]; then
        list="transmission-common_2.94-1mod1_all.deb
    transmission-cli_2.94-1mod1_amd64.deb
    transmission-daemon_2.94-1mod1_amd64.deb
    transmission-gtk_2.94-1mod1_amd64.deb
    transmission-qt_2.94-1mod1_amd64.deb
    transmission_2.94-1mod1_all.deb"
        mkdir -p /tmp/tr_deb
        cd /tmp/tr_deb
        for deb in $list ; do
            deb-get transmission $deb install
        done
        cd
        apt-mark hold transmission-common transmission-cli transmission-daemon transmission-gtk transmission-qt transmission
    else
        apt-get install -y libcurl4-openssl-dev libglib2.0-dev libevent-dev libminiupnpc-dev libgtk-3-dev libappindicator3-dev # > /dev/null
        apt-get install -y pkg-config automake autoconf cmake libtool intltool build-essential # 也不知道要不要，保险起见先装上去了
        apt-get install -y openssl
        [[ $CODENAME == stretch ]] && apt-get install -y libssl1.0-dev # https://tieba.baidu.com/p/5532509017?pn=2#117594043156l

        cd $SourceLocation
        wget -nv -N https://github.com/libevent/libevent/archive/release-2.1.8-stable.tar.gz
        tar xf release-2.1.8-stable.tar.gz ; rm -rf release-2.1.8-stable.tar.gz
        mv libevent-release-2.1.8-stable libevent-2.1.8
        cd libevent-2.1.8
        ./autogen.sh
        ./configure
        make -j$MAXCPUS
        make install

        ldconfig # ln -s /usr/local/lib/libevent-2.1.so.6 /usr/lib/libevent-2.1.so.6
        cd ..
        git clone https://github.com/transmission/transmission transmission-$tr_version
        cd transmission-$tr_version
        git checkout $tr_version
        # 修复 Transmission 2.92 无法在 Ubuntu 18.04 下编译的问题（openssl 1.1.0），https://github.com/transmission/transmission/pull/24
        [[ $tr_version == 2.92 ]] && { git config --global user.email "you@example.com" ; git config --global user.name "Your Name" ; git cherry-pick eb8f500 -m 1 ; }
        # 修复 2.93 以前的版本可能无法过 configure 的问题，https://github.com/transmission/transmission/pull/215
        grep m4_copy_force m4/glib-gettext.m4 -q || sed -i "s/m4_copy/m4_copy_force/g" m4/glib-gettext.m4
        ./autogen.sh
        ./configure --prefix=/usr
        make -j$MAXCPUS
        make install
    fi

    echo 1 | bash -c "$(wget -qO- https://github.com/ronggang/transmission-web-control/raw/master/release/install-tr-control.sh)"
    touch $LOCKLocation/transmission.lock
}

function install_x2go() {
    apt-get install -y xfce4 xfce4-goodies fonts-noto xfonts-intl-chinese-big xfonts-wqy

    if [[ $DISTRO == Ubuntu ]]; then
        apt-get install -y software-properties-common firefox
        apt-add-repository -y ppa:x2go/stable
    elif [[ $DISTRO == Debian ]]; then
        cat << EOF > /etc/apt/sources.list.d/x2go.list
    # X2Go Repository (release builds)
    deb http://packages.x2go.org/debian ${CODENAME} main
    # X2Go Repository (sources of release builds)
    deb-src http://packages.x2go.org/debian ${CODENAME} main
    # X2Go Repository (nightly builds)
    #deb http://packages.x2go.org/debian ${CODENAME} heuler
    # X2Go Repository (sources of nightly builds)
    #deb-src http://packages.x2go.org/debian ${CODENAME} heuler
EOF
        # gpg --keyserver http://keyserver.ubuntu.com --recv E1F958385BFE2B6E
        # gpg --export E1F958385BFE2B6E > /etc/apt/trusted.gpg.d/x2go.gpg
        apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E1F958385BFE2B6E
        APT_UPGRADE
        apt-get -y install x2go-keyring iceweasel
    fi

    APT_UPGRADE
    apt-get -y install x2goserver x2goserver-xsession pulseaudio

    touch $LOCKLocation/x2go.lock
}

function install_tools() {
    ########## NConvert ##########
    cd $SourceLocation
    wget -t1 -T5 -nv -N http://download.xnview.com/NConvert-linux64.tgz && {
        tar zxf NConvert-linux64.tgz
        mv NConvert/nconvert /usr/local/bin
        rm -rf NConvert*
    }
    ########## Blu-ray script ##########
    bash <(wget -qO- git.io/bluray) -u
    ########## ffmpeg ########## https://johnvansickle.com/ffmpeg/
    if [[ -z $(command -v ffmpeg) ]]; then
        mkdir -p /log/inexistence/ffmpeg && cd /log/inexistence/ffmpeg && rm -rf *
        wget -t2 -T5 -nv -N https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz
        tar xf ffmpeg-release-amd64-static.tar.xz
        rm -f ffmpeg-release-amd64-static.tar.xz
        cd ffmpeg*
        cp -f {ffmpeg,ffprobe,qt-faststart} /usr/bin
        cd && rm -rf /log/inexistence/ffmpeg
    fi
    ########## mkvtoolnix ##########
    wget -qO- https://mkvtoolnix.download/gpg-pub-moritzbunkus.txt | apt-key add -
    echo "deb https://mkvtoolnix.download/${DISTROL}/ $CODENAME main" > /etc/apt/sources.list.d/mkvtoolnix.list
    echo "deb-src https://mkvtoolnix.download/${DISTROL}/ $CODENAME main" >> /etc/apt/sources.list.d/mkvtoolnix.list

    apt-get -y update
    apt-get install -y mkvtoolnix mkvtoolnix-gui imagemagick mktorrent
    ######################  eac3to  ######################
    cd /etc/abox/app
    wget -nv -N http://madshi.net/eac3to.zip
    unzip -qq eac3to.zip
    rm -rf eac3to.zip ; cd

    touch $LOCKLocation/tools.lock
    echo -e "\n\n\n${bailvse}Version${normal}${bold}${green}"
    mktorrent -h | head -n1
    mkvmerge --version
    echo "Mediainfo `mediainfo --version | grep Lib | cut -c17-`"
    echo "ffmpeg `ffmpeg 2>&1 | head -n1 | awk '{print $3}'`${normal}"
}

function install_bbr() {
    if [[ $bbrinuse == Yes ]]; then
        sleep 0
    elif [[ $bbrkernel == Yes && $bbrinuse == No ]]; then
        enable_bbr
    else
        bnx2_firmware
        bbr_kernel_4_11_12
        enable_bbr
    fi
    echo -e "\n\n${bailvse}  BBR-INSTALLATION-COMPLETED  ${normal}\n"
}

# 安装 4.11.12 的内核
function bbr_kernel_4_11_12() {
    if [[ $CODENAME == stretch ]]; then
        [[ ! `dpkg -l | grep libssl1.0.0` ]] && { echo -ne "\n  {bold}Installing libssl1.0.0 ...${normal} "
        # 2019.04.09 为毛要用 hk 的？估摸着我当时问毛球要的？
        echo -e "\ndeb http://ftp.hk.debian.org/debian jessie main\c" >> /etc/apt/sources.list
        apt-get update
        apt-get install -y libssl1.0.0
        sed  -i '/deb http:\/\/ftp\.hk\.debian\.org\/debian jessie main/d' /etc/apt/sources.list
        apt-get update ; }
    else
        [[ ! `dpkg -l | grep libssl1.0.0` ]] && { echo -ne "\n  ${bold}Installing libssl1.0.0 ...${normal} "  ; apt-get install -y libssl1.0.0 ; }
    fi

    wget -nv -N O 1.deb https://github.com/Aniverse/BitTorrentClientCollection/raw/master/Linux.Kernel/BBR/linux-headers-4.11.12-all.deb
    wget -nv -N O 2.deb https://github.com/Aniverse/BitTorrentClientCollection/raw/master/Linux.Kernel/BBR/linux-headers-4.11.12-amd64.deb
    wget -nv -N O 3.deb https://github.com/Aniverse/BitTorrentClientCollection/raw/master/Linux.Kernel/BBR/linux-image-4.11.12-generic-amd64.deb
    dpkg -i [123].deb
    rm -rf [123].deb
    update-grub
}

# 开启 BBR
function enable_bbr() {
    bbrname=bbr
    sed -i '/net.core.default_qdisc.*/d' /etc/sysctl.conf
    sed -i '/net.ipv4.tcp_congestion_control.*/d' /etc/sysctl.conf
    echo "net.core.default_qdisc = fq" >> /etc/sysctl.conf
    echo "net.ipv4.tcp_congestion_control = $bbrname" >> /etc/sysctl.conf
    sysctl -p
    touch $LOCKLocation/bbr.lock
}

# Install firmware for BCM NIC
function bnx2_firmware() {
    if lspci | grep -i bcm >/dev/null; then
        mkdir -p /lib/firmware/bnx2 && cd /lib/firmware/bnx2
        bnx2="bnx2-mips-09-6.2.1b.fw bnx2-mips-06-6.2.3.fw bnx2-rv2p-09ax-6.0.17.fw bnx2-rv2p-09-6.0.17.fw bnx2-rv2p-06-6.0.15.fw"
        for f in $bnx2 ; do
            wget -nv -N https://sourceforge.net/projects/inexistence/files/firmware/bnx2/$f/download -O $f
        done
    fi
}

################################################################################################
################################################################################################ Install actions
################################################################################################



function do_installation() {
    mkdir -p $LogLocation
    [[ -n $hostname ]] && echo "$hostname" > /etc/hostname
    [[ $InsBBR == Yes || $InsBBR == To\ be\ enabled ]] && { echo -ne "Configuring BBR ... \n\n\n" ; install_bbr 2>&1 | tee $LogLocation/02.bbr.log ; }

    if [[ -n $lt_version ]] && [[ $lt_version != system ]]; then
        if   [[ $lt_version == RC_1_0 ]]; then
            bash $local_packages/package/libtorrent-rasterbar --logbase $LogTimes -m deb
        elif [[ $lt_version == RC_1_1 ]]; then
            bash $local_packages/package/libtorrent-rasterbar --logbase $LogTimes -m deb3
        elif [[ $lt_version == RC_1_2 ]]; then
            bash $local_packages/package/libtorrent-rasterbar --logbase $LogTimes -b RC_1_2
        else
            bash $local_packages/package/libtorrent-rasterbar --logbase $LogTimes -v $lt_version
        fi
    fi

    if [[ -n $qb_version ]] && [[ $qb_version != no ]]; then
        if   [[ $qb_mode == static ]]; then
            bash $local_packages/package/qbittorrent/install -v $qb_version -m static --logbase $LogTimes
        elif [[ $qb_mode == source ]]; then
            bash $local_packages/package/qbittorrent/install -v $qb_version -m source --logbase $LogTimes
        else
            bash $local_packages/package/qbittorrent/install -v $qb_version           --logbase $LogTimes
        fi
        bash $local_packages/package/qbittorrent/configure -u $iUser -p $iPass -w 2017 -i 9002 --logbase $LogTimes
    fi

    if [[ -n $de_version ]] && [[ $de_version != no ]]; then
        echo -ne "Installing Deluge ... "
        write_current_logfile $LogLocation/03.de1.log
        { install_deluge > $LogLocation/03.de1.log 2>&1 ; } & spinner $!
        OutputLOG=$LogLocation/03.de1.log   status_done
        write_current_logfile $LogLocation/04.de2.log
        bash $local_packages/package/deluge/configure -u $iUser -p $iPass -w 8112 -i 58846 --logbase $LogTimes
    fi

    if [[ -n $rt_version ]] && [[ $rt_version != no ]]; then
        # echo -e "Installing rTorrent ... "
        lines
        install_rtorrent 2>&1 | tee $LogLocation/07.rt.log
        lines
    fi

    if [[ $InsFlood == Yes ]]; then
        write_current_logfile $LogLocation/07.flood.log
        echo -ne "Installing Flood ... "
        { install_flood > $LogLocation/07.flood.log 2>&1 ; } & spinner $!
        OutputLOG=$LogLocation/07.flood.log    status_done
    fi

    if [[ -n $tr_version ]] && [[ $tr_version != no ]]; then
        write_current_logfile $LogLocation/08.tr1.log
        echo -ne "Installing Transmission ... "
        { install_transmission > $LogLocation/08.tr1.log 2>&1 ; } & spinner $!
        OutputLOG=$LogLocation/08.tr1.log    status_done
        bash $local_packages/package/transmission/configure -u $iUser -p $iPass -w 9099 -i 52333 --logbase $LogTimes   $force_recover
    fi

    if [[ $InsFlex == Yes ]]; then
        bash $local_packages/package/flexget/install   --logbase $LogTimes
        bash $local_packages/package/flexget/configure --logbase $LogTimes -u $iUser -p $iPass -w 9566 $force_recover
    fi
    if [[ $InsRclone == Yes ]]; then
        bash $local_packages/package/rclone/install --logbase $LogTimes
        echo -ne "Installing gclone ... "
        bash <(wget -qO- https://git.io/gclone.sh) > /dev/null 2>&1  # 懒得做检查了，一般不太可能失败。其实也可以放到 rclone 脚本里，先不改了吧
        echo -e "${green}${bold}DONE${normal}"
    fi
    if [[ $InsWine == Yes ]]; then
        bash $local_packages/package/wine/install --logbase $LogTimes
    fi
    if [[ $InsMono == Yes ]]; then
        bash $local_packages/package/mono/install --logbase $LogTimes
    fi
    if [[ $InsVNC == Yes ]]; then
        bash /etc/inexistence/00.Installation/package/novnc/install   --logbase $LogTimes
        bash /etc/inexistence/00.Installation/package/novnc/configure --logbase $LogTimes -u $iUser -p $iPass   $force_recover
    fi
    if [[ $InsFB == Yes ]]; then
        bash /etc/inexistence/00.Installation/package/filebrowser     --logbase $LogTimes -w 7575  -u $iUser -p $iPass   $force_recover
    fi
    if [[ $InsX2Go == Yes ]]; then
        write_current_logfile $LogLocation/12.x2go.log
        echo -ne "Installing X2Go ... "
        { install_x2go > $LogLocation/12.x2go.log 2>&1 ; } & spinner $!
        OutputLOG=$LogLocation/12.x2go.log    status_done
    fi
    if [[ $InsTools  == Yes ]]; then
        write_current_logfile $LogLocation/13.tool.log
        echo -ne "Installing Uploading Toolbox ... "
        { install_tools > $LogLocation/13.tool.log 2>&1 ; } & spinner $!
        OutputLOG=$LogLocation/13.tool.log    status_done
    fi
    if [[ $UseTweaks == Yes ]]; then
        if [[ $quick != 1 ]]; then
            bash $local_packages/package/vnstat/install --logbase $LogTimes
        fi
        write_current_logfile $LogLocation/98.tweaks
        echo -ne  "Configuring system settings ..."
        OutputLOG=$LogLocation/98.tweaks    system_tweaks  & spinner $!
        OutputLOG=$LogLocation/98.tweaks    status_done
    fi

    export_inexistence_info
    create_log_link
    create_config_link
    chown $iUser:$iUser -R $iHome/inexistence
}











################################################################################################
################################################################################################ DEPRECATED
################################################################################################

function version_check_latest_and_repo() {
    # 2020.04.07 这个检查比较慢，以后干脆写死，提高速度
    # 有可能出现刚开的机器没有 apt update，直接 apt-cache policy 提示找不到包的情况
    QB_repo_ver=$(apt-cache policy qbittorrent-nox | grep -B1 http | grep -Eo "[234]\.[0-9.]+\.[0-9.]+" | head -1)
    [[ -z $QB_repo_ver ]] && { [[ $CODENAME == bionic ]] && QB_repo_ver=4.0.3 ; [[ $CODENAME == xenial ]] && QB_repo_ver=3.3.1 ; [[ $CODENAME == jessie ]] && QB_repo_ver=3.1.10 ; [[ $CODENAME == stretch ]] && QB_repo_ver=3.3.7 ; }
    
    QB_latest_ver=$(wget -qO- https://github.com/qbittorrent/qBittorrent/releases | grep releases/tag | grep -Eo "[45]\.[0-9.]+" | head -1)
    [[ -z $QB_latest_ver ]] && QB_latest_ver=4.2.3
    
    DE_repo_ver=$(apt-cache policy deluged | grep -B1 http | grep -Eo "[12]\.[0-9.]+\.[0-9.]+" | head -1)
    [[ -z $DE_repo_ver ]] && { [[ $CODENAME == bionic ]] && DE_repo_ver=1.3.15 ; [[ $CODENAME == xenial ]] && DE_repo_ver=1.3.12 ; [[ $CODENAME == jessie ]] && DE_repo_ver=1.3.10 ; [[ $CODENAME == stretch ]] && DE_repo_ver=1.3.13 ; }
    
    DE_latest_ver=$(wget -qO- https://dev.deluge-torrent.org/wiki/ReleaseNotes | grep wiki/ReleaseNotes | grep -Eo "[12]\.[0-9.]+" | sed 's/">/ /' | awk '{print $1}' | head -1)
    [[ -z $DE_latest_ver ]] && DE_latest_ver=2.0.3
    # DE_github_latest_ver=` wget -qO- https://github.com/deluge-torrent/deluge/releases | grep releases/tag | grep -Eo "[12]\.[0-9.]+.*" | sed 's/\">//' | head -n1 `
    
    TR_repo_ver=$(apt-cache policy transmission-daemon | grep -B1 http | grep -Eo "[23]\.[0-9.]+" | head -1)
    [[ -z $TR_repo_ver ]] && { [[ $CODENAME == bionic ]] && TR_repo_ver=2.92 ; [[ $CODENAME == xenial ]] && TR_repo_ver=2.84 ; [[ $CODENAME == jessie ]] && TR_repo_ver=2.84 ; [[ $CODENAME == stretch ]] && TR_repo_ver=2.92 ; }
    
    TR_latest_ver=$(wget -qO- https://github.com/transmission/transmission/releases | grep releases/tag | grep -Eo "[23]\.[0-9.]+" | head -1)
    [[ -z $TR_latest_ver ]] && TR_latest_ver=2.94
}
